package com.start.workflow.core;

import com.start.common.registers.BaseScanPackageRegister;
import com.start.common.tools.UUIdTools;
import com.start.workflow.core.listener.StartGlobalActActivityListener;
import com.start.workflow.core.mappers.StartActReDeploymentMapper;
import com.start.workflow.core.store.StartActDynamicFormStore;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.impl.cfg.IdGenerator;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.activiti.spring.boot.ProcessEngineConfigurationConfigurer;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * org.activiti.spring.boot.ProcessEngineAutoConfiguration.class,
 * org.activiti.spring.boot.EndpointAutoConfiguration.class
 */
@Slf4j
@Configuration
@ComponentScan(value = {"com.start.workflow.core.**"})
@MapperScan({"com.start.workflow.core.mappers.**"})
public class StartWorkFlowCoreAutoConfiguration implements CommandLineRunner, BaseScanPackageRegister, ProcessEngineConfigurationConfigurer, IdGenerator {
    @Bean
    @ConditionalOnBean(StartActFormListener.class)
    public StartActFormListener actFormListener() {
        return new StartActFormListener();
    }

    /**
     * 获取所在包
     *
     * @return
     */
    @Override
    public String[] getPackages() {
        return new String[]{"com.start.workflow.core"};
    }

    @Override
    public String getNextId() {
        return UUIdTools.createId32();
    }

    @Autowired
    private StartActReDeploymentMapper startActReDeploymentMapper;
    @Autowired
    private StartGlobalActActivityListener listener;

    /**
     * 配置activit 全局监听器
     *
     * @param springProcessEngineConfiguration
     */
    @Override
    public void configure(SpringProcessEngineConfiguration springProcessEngineConfiguration) {
        List<ActivitiEventListener> eventListeners = new ArrayList<>();
        eventListeners.add(listener);
        springProcessEngineConfiguration.setEventListeners(eventListeners);
    }

    /**
     * 删除activi 每次启动是自动添加默认流程
     *
     * @param args
     */
    @Override
    public void run(String... args) {
        try {
            long rowNum = startActReDeploymentMapper.deleteSpringAutoDeployment();
            log.info("删除activiti 无效流程" + rowNum);
        } catch (Exception e) {
        }
    }

    /**
     * 动态表单引擎数据表加载器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public StartActDynamicFormStore startActFormEngineLoad() {
        return new StartActDynamicFormStore();
    }
}
